<?php
	include_once("common.php");
	include_once("script/articles.php");
	
	if(isVisitor() && !isset($_GET['id']))
		header("Location: index.php");

	$article = getArticle($_GET['id']);
	$authors = getAuthors($_GET['id']);
	$comments = getComments($_GET['id']);

	$owner = $_SESSION['idusers'] == $article['owner'];
	$author = isAuthor($_SESSION['idusers'], $article);
?>
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>COMP 353 Project</title>

    <!-- Bootstrap core CSS -->
    <link href="./css/bootstrap.min.css" rel="stylesheet">

    <!-- Custom styles for this template -->
    <link href="./css/template.css" rel="stylesheet">

    <!-- Just for debugging purposes. Don't actually copy this line! -->
    <!--[if lt IE 9]><script src="../../docs-assets/js/ie8-responsive-file-warning.js"></script><![endif]-->

    <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
    <![endif]-->
</head>
<body>
	<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
		<div class="container">
			<div class="navbar-header">
				<a class="brand" href="#">SEMS Lite</a>
			</div>
			<div class="navbar">
				<ul class="nav navbar-nav">
					<li><a href="index.php">Home</a></li>
					<li><a href="search.php">Search</a></li>
					<li><a href="signup.php">Signup</a></li>
				</ul>
<?php
	if(isVisitor()){
		echo '<form class="navbar-form pull-right" method="post" action="index.php">'."\n",
			'<input name="username" type="text" placeholder="Username" class="form-control">'."\n",
			'<input name="password" type="password" placeholder="Password" class="form-control">'."\n",
			'<button type="submit" class="btn">Sign in</button>'."\n",
			'</form>'."\n";
	}
	else{
		echo '<form class="navbar-form pull-right" method="post" action="index.php">'."\n",
			'<input type="submit" class="btn" name="logout" value="Logout">'."\n",
			'</form>'."\n";
	}
?>
			</div>
		</div>
	</div>
<!-- CONTENT -->
<?php
	if(isset($_SESSION['username']) && isAdmin($_SESSION['username']))
		include("script/admin_menu.php");

	if(!isVisitor())
		include_once("script/submenu.php");
?>
	<div class="container">
		<h1><?php echo $article['title']; ?></h1>
		<p class="align-right muted"><em><?php echo $article['file_name']." - ".$article['file_size']; ?> Bytes</em></p>
		<p><?php echo $article['abstract']; ?></p>
<?php
	echo '<a href="data/'.$article['file_name'].'" target="_blank" class="btn btn-primary pull-right">Download</a>';
?>
		<h3>Authors</h3>
		<table class="table">
			<tr><td><?php echo $article['first_name']; ?></td><td><?php echo $article['last_name']; ?></td></tr>
<?php
	foreach($authors as $a)
		echo "<tr><td>".$a['first_name']."</td><td>".$a['last_name']."</td></tr>";

	if($owner)
		echo '<tr>
		<td><input type="text" placeholder="Username" 
		id="inputAuthorName" autocomplete="off" 
		data-provide="typeahead" data-items="4"></td>
		<td><button class="btn" id="addAuthor">Add</button></td>
		</tr>';
?>
		</table>
		<h3>Comments</h3>
<?php
	foreach($comments as $c)
		echo '<div class="container well">
		<p class="align-right"><em>'.$c['marker'].' '.$c['event'].'</em></p
		><p>'.$c['comment'].'</p>
		</div>';
?>
    </div>
    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
	<!-- CDN -->
	<!-- <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> -->
	<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/jquery-ui.min.js"></script>
	<!-- LOCAL -->
    <script src="./js/jquery-1.10.2.min.js"></script>
    <script src="./js/bootstrap.min.js"></script>
	<script type="text/javascript">
		$(".submenu .nav li:nth-child(2)").addClass("active");

		$("#addAuthor").click(function(){
			var c_name = $("#inputAuthorName").val();
			console.log(c_name);
			if(c_name != ""){
				$.ajax({url: "script/addAuthor.php", data: {name: c_name,
					id: "<?php echo $_GET['id']; ?>"
				}})
					.done(function(reply){
						var data = reply.split(",");
						$("#authorTable").append(
							'<tr>'+
							'<td>'+$data[2]+'</td><td>'+$data[3]+'</td>'+
							'</tr>'
						);
					});
			}
		});

		$("#inputAuthorName").typeahead({source: function(txt, cb){
			if(txt.length> 2){
				$.ajax({url: "script/event_setting_getUser.php", data: {name: txt}}).
					done(function(reply){
						var data = reply.split(",");
						cb(data);
					});
			}
		}});
	</script>
  </body>
</html>
